package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.CashRecharge;
import com.chen.dto.R;
import com.chen.model.CoinModel;
import com.chen.service.CashRechargeService;
import com.chen.utils.ReportCsvUtils;
import com.chen.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.supercsv.cellprocessor.CellProcessorAdaptor;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.util.CsvContext;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: chen
 * @Date: 2021/5/11 2:09 PM
 * @Version 1.0
 */
@RestController
@RequestMapping("/cashRecharges")
@Api(tags = "GCN充值控制器")
public class CashRechargeController {

    @Autowired
    private CashRechargeService cashRechargeService;


    @PostMapping("/buy")
    public R<CashRecharge> buy(@RequestBody CoinModel coinModel){
        CashRecharge buy = cashRechargeService.buy(coinModel);
        return R.ok(buy);
    }

    @GetMapping("/user/records")
    public R<Page<CashRecharge>> listForUser(Page<CashRecharge> page,Integer status){
        QueryWrapper<CashRecharge> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", UserUtil.getUser());
        queryWrapper.eq(status != null,"status",status);
        queryWrapper.orderByDesc("created");
        Page<CashRecharge> page1 = cashRechargeService.page(page, queryWrapper);
        return R.ok(page1);
    }

    @PostMapping("/cashRechargeUpdateStatus")
    public R check(@RequestBody CashRecharge cashRecharge) {
        cashRechargeService.checkRecharge(cashRecharge);
        return R.ok();
    }


    @GetMapping("/records")
    @ApiOperation(value = "条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "coinId", value = "币种的Id"),
            @ApiImplicitParam(name = "userId", value = "用户的Id"),
            @ApiImplicitParam(name = "userName", value = "用户的名称"),
            @ApiImplicitParam(name = "mobile", value = "用户的手机号"),
            @ApiImplicitParam(name = "status", value = "充值审核状态"),
            @ApiImplicitParam(name = "numMin", value = "充值最小金额"),
            @ApiImplicitParam(name = "numMax", value = "充值最大金额"),
            @ApiImplicitParam(name = "startTime", value = "充值开始时间"),
            @ApiImplicitParam(name = "endTime", value = "充值结束时间"),

    })
    public R<Page<CashRecharge>> findByPage(
            @ApiIgnore Page<CashRecharge> page,
            Long coinId, Long userId, String userName,
            String mobile, Byte status, String numMin, String numMax,
            String startTime, String endTime
    ) {

        Page<CashRecharge> rechargePage = cashRechargeService.pageForList(page, coinId, userId, userName, mobile, status, numMax, numMin, startTime, endTime);
        return R.ok(rechargePage);
    }

    @GetMapping("/exportCNYRecharge")
    @ApiOperation(value = "条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "coinId", value = "币种的Id"),
            @ApiImplicitParam(name = "userId", value = "用户的Id"),
            @ApiImplicitParam(name = "userName", value = "用户的名称"),
            @ApiImplicitParam(name = "mobile", value = "用户的手机号"),
            @ApiImplicitParam(name = "status", value = "充值审核状态"),
            @ApiImplicitParam(name = "numMin", value = "充值最小金额"),
            @ApiImplicitParam(name = "numMax", value = "充值最大金额"),
            @ApiImplicitParam(name = "startTime", value = "充值开始时间"),
            @ApiImplicitParam(name = "endTime", value = "充值结束时间"),

    })
    public void exportCNYRecharge(
            Long coinId, Long userId, String userName,
            String mobile, Byte status, String numMin, String numMax,
            String startTime, String endTime, HttpServletResponse response
    ) throws IOException {

        Page<CashRecharge> page = new Page<>(1, 10000);
        Page<CashRecharge> pageData = cashRechargeService.pageForList(page, coinId, userId, userName,
                mobile, status, numMin, numMax, startTime, endTime);
        List<CashRecharge> records = pageData.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            String[] header = {"ID", "用户ID", "用户名", "真实用户名", "充值币种", "充值金额(USDT)", "手续费", "到账金额(CNY)", "充值方式", "充值订单", "参考号", "充值时间", "完成时间", "状态", "审核备注", "审核级数"};
            String[] properties = {"id", "userId", "username", "realName", "coinName", "num", "fee", "mum", "type", "tradeno", "remark", "created", "lastTime", "status", "auditRemark", "step"};

            CellProcessorAdaptor longToStringAdapter = new CellProcessorAdaptor() {
                @Override
                public <T> T execute(Object o, CsvContext csvContext) {
                    return (T) String.valueOf(o);
                }
            };
            // 对于金额,需要8位有效数字,金额
            DecimalFormat decimalFormat = new DecimalFormat("0.00000000");


            CellProcessorAdaptor moneyCellProcessorAdaptor = new CellProcessorAdaptor() {
                @Override
                public <T> T execute(Object o, CsvContext csvContext) {
                    BigDecimal num = (BigDecimal) o;
                    String numReal = decimalFormat.format(num);
                    return (T) numReal;
                }
            };
            //      @ApiModelProperty(value = "类型：alipay，支付宝；cai1pay，财易付；bank，银联；")
            CellProcessorAdaptor typeAdapter = new CellProcessorAdaptor() {
                @Override
                public <T> T execute(Object o, CsvContext csvContext) {
                    String type = String.valueOf(o);
                    String typeName = "";
                    switch (type) {
                        case "alipay":
                            typeName = "支付宝";
                            break;
                        case "cai1pay":
                            typeName = "财易付";
                            break;
                        case "bank":
                            typeName = "银联";
                            break;
                        case "linepay":
                            typeName = "在线支付";
                            break;
                        default:
                            typeName = "未知";
                            break;

                    }
                    return (T) typeName;
                }
            };
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            CellProcessorAdaptor timeCellProcessorAdaptor = new CellProcessorAdaptor() {
                @Override
                public <T> T execute(Object o, CsvContext csvContext) {
                    if (o == null) {
                        return (T) "";
                    }
                    Date date = (Date) o;
                    String dateStr = simpleDateFormat.format(date);
                    return (T) dateStr;
                }
            };
            // 0-待审核；1-审核通过；2-拒绝；3-充值成功
            CellProcessorAdaptor statusCellProcessorAdaptor = new CellProcessorAdaptor() {
                @Override
                public <T> T execute(Object o, CsvContext csvContext) {
                    Integer status = Integer.valueOf(String.valueOf(o));
                    String statusStr = "";
                    switch (status) {
                        case 0:
                            statusStr = "待审核";
                            break;
                        case 1:
                            statusStr = "审核通过";
                            break;
                        case 2:
                            statusStr = "拒绝";
                            break;
                        case 3:
                            statusStr = "充值成功";
                            break;
                        default:
                            statusStr = "未知";
                            break;

                    }
                    return (T) statusStr;
                }
            };

//            String[] header = {"ID", "用户ID", "用户名", "真实用户名", "充值币种", "充值金额(USDT)", "手续费", "到账金额(CNY)", "充值方式", "充值订单", "参考号", "充值时间", "完成时间", "状态", "审核备注", "审核级数"};
            CellProcessor[] PROCESSOR = new CellProcessor[]{
                    longToStringAdapter, longToStringAdapter, null, null, null, // "ID", "用户ID", "用户名", "真实用户名", "充值币种",
                    moneyCellProcessorAdaptor, moneyCellProcessorAdaptor, moneyCellProcessorAdaptor, typeAdapter,  // "充值金额(USDT)", "手续费", "到账金额(CNY)", 充值方式"
                    null, null, timeCellProcessorAdaptor, timeCellProcessorAdaptor,//充值订单", "参考号", "充值时间", "完成时间
                    statusCellProcessorAdaptor, null, null //状态 "审核备注", "审核级数"

            };
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

            try {
                // 导出csv 文件
                ReportCsvUtils.reportListCsv(requestAttributes.getResponse(), header, properties, "场外交易充值审核.csv", records, PROCESSOR);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}
